#include "widget.h"
#include <QApplication>
#include <QLineEdit>
#include <QPushButton>
#include <QSpacerItem>
#include <QDate>
#include <QGridLayout>
#include <QMap>
#include <QDebug>
#include <QPainter>

#define INCREMENT     3   // 温度每升高/降低1度，y轴坐标的增量
#define POINT_RADIUS  3   // 曲线描点的大小
#define TEXT_OFFSET_X 8  // 温度文本相对于点的偏移
#define TEXT_OFFSET_Y 10  // 温度文本相对于点的偏移

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    _exitMenu = new QMenu(this);
    _exitAction = new QAction(_exitMenu);
    _exitAction->setText("退出");
    _exitAction->setIcon(QIcon(":/res/close.png"));
    _exitMenu->addAction(_exitAction);
    connect(_exitAction, &QAction::triggered, [=]() {
        qApp->exit(0);
    });

    this->setObjectName("weather_object");
    this->setStyleSheet(R"(
        QWidget#weather_object {
            background-color: rgba(50, 115, 165, 255);
        }
        QLabel {
            font: 12px "Microsoft YaHei";
            color: rgb(255, 255, 255);
            padding: 12px;
        }
    )");

    // 1.创建主体布局
    QVBoxLayout* vl = new QVBoxLayout(this);
    vl->setSpacing(0);
    vl->setContentsMargins(0, 0, 0, 0);
    QWidget* mainWidget = new QWidget(this);
    vl->addWidget(mainWidget);

    _mainLayout = new QVBoxLayout(mainWidget);
    _mainLayout->setSpacing(10);
    _mainLayout->setContentsMargins(18, 18, 18, 18);

    // 2.顶部布局
    _topWidget = new QWidget(mainWidget);
    _topLayout = new QHBoxLayout(_topWidget);
    _mainLayout->addWidget(_topWidget);

    // 3.底部布局
    QWidget* bottomWidget = new QWidget(mainWidget);
    QHBoxLayout* bottomLayout = new QHBoxLayout(bottomWidget);

    _leftWidget = new QWidget(bottomWidget);
    _leftLayout = new QVBoxLayout(_leftWidget);
    _leftLayout->setContentsMargins(0, 0, 0, 0);

    _rightWidget = new QWidget(bottomWidget);
    _rightLayout = new QVBoxLayout(_rightWidget);
    _rightLayout->setSpacing(16);

    bottomLayout->addWidget(_leftWidget);
    bottomLayout->addWidget(_rightWidget);
    bottomLayout->setStretch(0, 2);
    bottomLayout->setStretch(1, 4);
    _mainLayout->addWidget(bottomWidget);

    initTop();
    initLeft();
    initRight();

    _netWork = new WeatherNetWork(this);
}

Widget::~Widget() {}

void Widget::contextMenuEvent(QContextMenuEvent* event)
{
    _exitMenu->exec(QCursor::pos());
    event->accept();
}

void Widget::mousePressEvent(QMouseEvent* event)
{
    _offset = event->globalPosition().toPoint() - this->pos();
}

void Widget::mouseMoveEvent(QMouseEvent* event)
{
    this->move(event->globalPosition().toPoint() - _offset);
}

bool Widget::eventFilter(QObject* watched, QEvent* event)
{
    if (watched == _highTempLineChart && event->type() == QEvent::Paint) {
        this->drawHighTempCurve();
    }
    if (watched == _lowTempLineChart && event->type() == QEvent::Paint) {
        this->drawLowTempCurve();
    }
    return QWidget::eventFilter(watched, event);
}

void Widget::initTop()
{
    _cityEdit = new QLineEdit(_topWidget);
    _cityEdit->setFixedWidth(360);
    _cityEdit->setStyleSheet(R"(
        font: 14px Microsoft YaHei;
        background-color: rgb(255, 255, 255);
        border-radius: 4px;
        padding: 4px 8px;
    )");
    _topLayout->addWidget(_cityEdit);

    // 搜索按钮
    _searchButton = new QPushButton(_topWidget);
    _searchButton->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    _searchButton->setIcon(QIcon(":/res/search.png"));
    _searchButton->setIconSize(QSize(24, 24));
    connect(_searchButton, &QPushButton::clicked, this, &Widget::onSearchButtonClick);

    _topLayout->addWidget(_searchButton);

    // 弹簧
    QSpacerItem* spacerItem = new QSpacerItem(32, 32, QSizePolicy::Expanding, QSizePolicy::Minimum);
    _topLayout->addSpacerItem(spacerItem);

    // 日期
    _labelDate = new QLabel(_topWidget);
    _labelDate->setStyleSheet(R"(
        font: 20px Arial;
        background-color: rgba(255, 255, 255, 0);
    )");
    _labelDate->setAlignment(Qt::AlignCenter);
    QString dateStr;
    this->getDate(dateStr);
    _labelDate->setText(dateStr);
    _topLayout->addWidget(_labelDate);
}

void Widget::initLeft()
{
    // 1 天气 温度 城市
    QWidget* widget = new QWidget(_leftWidget);
    QHBoxLayout* hl1 = new QHBoxLayout(widget);
    hl1->setSpacing(0);

    // 1.1 天气
    _weatherIcon = new QLabel(widget);
    _weatherIcon->setFixedSize(150, 150);
    _weatherIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    _weatherIcon->setPixmap(QPixmap(":/res/DuoYun.png"));
    _weatherIcon->setScaledContents(true);
    _weatherIcon->setAlignment(Qt::AlignCenter);
    hl1->addWidget(_weatherIcon);

    // 温度 城市 天气 高低温 整体垂直布局
    QVBoxLayout* vl1 = new QVBoxLayout();
    hl1->addLayout(vl1);

    // 1.2 温度
    _temperatureText = new QLabel(widget);
    _temperatureText->setStyleSheet(R"(
        font: 50px "Arial";
        background-color: rgba(255, 255, 255, 0);
        padding: 0px;
    )");
    _temperatureText->setText("23°");
    _temperatureText->setAlignment(Qt::AlignCenter);
    _temperatureText->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl1->addWidget(_temperatureText);

    // 城市 天气 高低温 水平布局
    QHBoxLayout* hl2 = new QHBoxLayout();
    vl1->addLayout(hl2);

    // 1.3 城市
    _city = new QLabel(widget);
    _city->setText("广州");
    _city->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 12px "Microsoft YaHei";
        padding: 0px 0px 24px 0px;
    )");
    _city->setAlignment(Qt::AlignCenter);
    _city->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hl2->addWidget(_city);

    // 弹簧
    QSpacerItem* item1 = new QSpacerItem(16, 1, QSizePolicy::Fixed, QSizePolicy::Minimum);
    hl2->addItem(item1);

    // 1.4 天气
    _weatherText = new QLabel(widget);
    _weatherText->setText("多云");
    _weatherText->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 12px "Microsoft YaHei";
        padding: 0px 0px 24px 0px;
    )");
    _weatherText->setAlignment(Qt::AlignCenter);
    _weatherText->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hl2->addWidget(_weatherText);

    // 弹簧
    QSpacerItem* item2 = new QSpacerItem(16, 1, QSizePolicy::Fixed, QSizePolicy::Minimum);
    hl2->addItem(item2);

    // 1.5 高低温
    _lowHighTemperature = new QLabel(widget);
    _lowHighTemperature->setText("11~29°C");
    _lowHighTemperature->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 12px "Microsoft YaHei";
        padding: 0px 0px 24px 0px;
    )");
    _lowHighTemperature->setAlignment(Qt::AlignCenter);
    _lowHighTemperature->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hl2->addWidget(_lowHighTemperature);

    _leftLayout->addWidget(widget);

    // 弹簧
    QSpacerItem* item3 = new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Expanding);
    _leftLayout->addSpacerItem(item3);

    // 2.感冒指数
    _coldIndex = new QLabel(widget);
    _coldIndex->setText("感冒指数：极少数敏感人群应减少户外活动");
    _coldIndex->setStyleSheet(R"(
        background-color: rgba(60, 60, 60, 0);
        padding-left: 5px;
        padding-right: 5px;
        font: 12px "Microsoft YaHei"
    )");
    // 可以换行
    _coldIndex->setWordWrap(true);
    _leftLayout->addWidget(_coldIndex);

    // 3.风力 PM2.5 湿度 空气质量
    QWidget* gridWidget = new QWidget(widget);
    gridWidget->setStyleSheet(R"(
        background-color: rgb(157, 133, 255);
        border-radius: 15px
    )");
    QGridLayout* gridLayout = new QGridLayout(gridWidget);
    gridLayout->setHorizontalSpacing(40);
    gridLayout->setVerticalSpacing(30);
    gridLayout->setContentsMargins(40, 25, 40, 25);
    _leftLayout->addWidget(gridWidget);

    // 3.1 风力 整体采用水平布局
    QHBoxLayout* hl3 = new QHBoxLayout();
    // 第0行第0列 占1行1列
    gridLayout->addLayout(hl3, 0, 0, 1, 1);

    // 风力图标
    _windIcon = new QLabel(gridWidget);
    _windIcon->setFixedSize(72, 72);
    _windIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    _windIcon->setPixmap(QPixmap(":/res/wind.png"));
    _windIcon->setScaledContents(true);
    _windIcon->setAlignment(Qt::AlignCenter);
    hl3->addWidget(_windIcon);

    // 风向和风力级别 采用垂直布局
    QVBoxLayout* vl2 = new QVBoxLayout();
    hl3->addLayout(vl2);

    // 风向
    _windDirection = new QLabel(gridWidget);
    _windDirection->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 10px "Microsoft YaHei";
        padding: 24px 0px 0px 0px;
    )");
    _windDirection->setText("东风");
    _windDirection->setAlignment(Qt::AlignCenter);
    _windDirection->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl2->addWidget(_windDirection);

    // 风力级别
    _windLevel = new QLabel(gridWidget);
    _windLevel->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: bold 12px "Microsoft YaHei";
        padding: 0px 0px 24px 0px;
    )");
    _windLevel->setText("2级");
    _windLevel->setAlignment(Qt::AlignCenter);
    _windLevel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl2->addWidget(_windLevel);

    // 3.2 PM2.5 整体采用水平布局
    QHBoxLayout* hl4 = new QHBoxLayout();
    // 第0行第1列 占1行1列
    gridLayout->addLayout(hl4, 0, 1, 1, 1);

    // PM2.5图标
    _pM25Icon = new QLabel(gridWidget);
    _pM25Icon->setFixedSize(72, 72);
    _pM25Icon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    _pM25Icon->setPixmap(QPixmap(":/res/pm25.png"));
    _pM25Icon->setScaledContents(true);
    _pM25Icon->setAlignment(Qt::AlignCenter);
    hl4->addWidget(_pM25Icon);

    // PM2.5和指数 采用垂直布局
    QVBoxLayout* vl3 = new QVBoxLayout();
    hl4->addLayout(vl3);

    // PM2.5
    _pM25Text = new QLabel(gridWidget);
    _pM25Text->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 10px "Microsoft YaHei";
        padding: 24px 0px 0px 0px;
    )");
    _pM25Text->setText("PM2.5");
    _pM25Text->setAlignment(Qt::AlignCenter);
    _pM25Text->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl3->addWidget(_pM25Text);

    // PM2.5指数
    _pM25Index = new QLabel(gridWidget);
    _pM25Index->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: bold 12px "Microsoft YaHei";
        padding: 0px 0px 24px 0px;
    )");
    _pM25Index->setText("47");
    _pM25Index->setAlignment(Qt::AlignCenter);
    _pM25Index->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl3->addWidget(_pM25Index);

    // 3.3 湿度 整体采用水平布局
    QHBoxLayout* hl5 = new QHBoxLayout();
    // 第1行第0列 占1行1列
    gridLayout->addLayout(hl5, 1, 0, 1, 1);

    // 湿度图标
    _humidityIcon = new QLabel(gridWidget);
    _humidityIcon->setFixedSize(72, 72);
    _humidityIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    _humidityIcon->setPixmap(QPixmap(":/res/humidity.png"));
    _humidityIcon->setScaledContents(true);
    _humidityIcon->setAlignment(Qt::AlignCenter);
    hl5->addWidget(_humidityIcon);

    // 湿度和百分比 采用垂直布局
    QVBoxLayout* vl4 = new QVBoxLayout();
    hl5->addLayout(vl4);

    // 湿度
    _humidityText = new QLabel(gridWidget);
    _humidityText->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 10px "Microsoft YaHei";
        padding: 24px 0px 0px 0px;
    )");
    _humidityText->setText("湿度");
    _humidityText->setAlignment(Qt::AlignCenter);
    _humidityText->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl4->addWidget(_humidityText);

    // 湿度百分比
    _humidityPercent = new QLabel(gridWidget);
    _humidityPercent->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: bold 12px "Microsoft YaHei";
        padding: 0px 0px 24px 0px;
    )");
    _humidityPercent->setText("77%");
    _humidityPercent->setAlignment(Qt::AlignCenter);
    _humidityPercent->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl4->addWidget(_humidityPercent);

    // 3.4 空气质量 整体采用水平布局
    QHBoxLayout* hl6 = new QHBoxLayout();
    // 第1行第1列 占1行1列
    gridLayout->addLayout(hl6, 1, 1, 1, 1);

    // 空气质量图标
    _airQualityIcon = new QLabel(gridWidget);
    _airQualityIcon->setFixedSize(72, 72);
    _airQualityIcon->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    _airQualityIcon->setPixmap(QPixmap(":/res/aqi.png"));
    _airQualityIcon->setScaledContents(true);
    _airQualityIcon->setAlignment(Qt::AlignCenter);
    hl6->addWidget(_airQualityIcon);

    // 空气质量和空气指数 采用垂直布局
    QVBoxLayout* vl5 = new QVBoxLayout();
    hl6->addLayout(vl5);

    // 空气质量
    _airQualityText = new QLabel(gridWidget);
    _airQualityText->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 10px "Microsoft YaHei";
        padding: 24px 0px 0px 0px;
    )");
    _airQualityText->setText("空气质量");
    _airQualityText->setAlignment(Qt::AlignCenter);
    _airQualityText->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl5->addWidget(_airQualityText);

    // 空气质量指数
    _airQualityIndex = new QLabel(gridWidget);
    _airQualityIndex->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: bold 12px "Microsoft YaHei";
        padding: 0px 0px 24px 0px;
    )");
    _airQualityIndex->setText("24");
    _airQualityIndex->setAlignment(Qt::AlignCenter);
    _airQualityIndex->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    vl5->addWidget(_airQualityIndex);
}

void Widget::initRight()
{
    // 1.星期 日期
    QGridLayout* gridLayoutDate = new QGridLayout();
    gridLayoutDate->setHorizontalSpacing(6);
    gridLayoutDate->setVerticalSpacing(0);
    _rightLayout->addLayout(gridLayoutDate);


    for (int i = 0; i < 6; ++i) {
        QLabel* week = new QLabel(_rightWidget);
        QLabel* date = new QLabel(_rightWidget);
        week->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        date->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

        week->setStyleSheet(R"(
            border-bottom-left-radius: 0px;
            border-bottom-right-radius: 0px;
            padding-bottom: 2px;
            padding-left: 20px;
            padding-right: 20px;
            background-color: rgb(10, 180, 190);
            font: 10px "Microsoft YaHei";
        )");
        date->setStyleSheet(R"(
            border-top-left-radius: 0px;
            border-top-right-radius: 0px;
            padding-top: 2px;
            padding-left: 20px;
            padding-right: 20px;
            background-color: rgb(10, 180, 190);
        )");
        week->setAlignment(Qt::AlignCenter);
        date->setAlignment(Qt::AlignCenter);

        gridLayoutDate->addWidget(week, 0, i);
        gridLayoutDate->addWidget(date, 1, i);

        _weekList << week;
        _dateList << date;
    }

    // 2.天气图标 天气类型
    QGridLayout* gridLayoutWeather = new QGridLayout();
    gridLayoutWeather->setHorizontalSpacing(6);
    gridLayoutWeather->setVerticalSpacing(0);
    _rightLayout->addLayout(gridLayoutWeather);

    for (int i = 0; i < 6; ++i) {
        QLabel* weatherIcon = new QLabel(_rightWidget);
        QLabel* weatherText = new QLabel(_rightWidget);
        weatherIcon->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        weatherText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

        weatherIcon->setStyleSheet(R"(
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
            border-bottom-left-radius: 0px;
            border-bottom-right-radius: 0px;
            padding-bottom: 2px;
            padding-left: 20px;
            padding-right: 20px;
            background-color: rgb(10, 180, 190);
        )");
        weatherText->setStyleSheet(R"(
            border-top-left-radius: 0px;
            border-top-right-radius: 0px;
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
            padding-top: 2px;
            padding-left: 20px;
            padding-right: 20px;
            background-color: rgb(10, 180, 190);
            font: 8px "Microsoft YaHei";
        )");
        weatherIcon->setAlignment(Qt::AlignCenter);
        weatherText->setAlignment(Qt::AlignCenter);

        gridLayoutWeather->addWidget(weatherIcon, 0, i);
        gridLayoutWeather->addWidget(weatherText, 1, i);

        _weatherIconList << weatherIcon;
        _weatherTypeList << weatherText;
    }

    // 3.空气质量等级
    QWidget* qualityWidget = new QWidget(_rightWidget);
    qualityWidget->setStyleSheet("background-color: rgba(51, 115, 163, 255); border-radius: 4px;");
    qualityWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QHBoxLayout* qualityLayout = new QHBoxLayout(qualityWidget);
    qualityLayout->setSpacing(6);
    qualityLayout->setContentsMargins(0, 0, 0, 0);

    _rightLayout->addWidget(qualityWidget);

    for (int i = 0; i < 6; ++i) {
        QLabel* qualityLabel = new QLabel(_rightWidget);
        qualityLabel->setAlignment(Qt::AlignCenter);
        qualityLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        _airQualityList << qualityLabel;
        qualityLayout->addWidget(qualityLabel);
    }

    // 4.温度曲线
    QVBoxLayout* tempCurveLayout = new QVBoxLayout();
    _rightLayout->addLayout(tempCurveLayout);
    tempCurveLayout->setSpacing(0);

    _highTempLineChart = new QLabel(_rightWidget);
    _highTempLineChart->setMinimumHeight(100);
    _highTempLineChart->setStyleSheet(R"(
        padding-bottom: 2px;
        padding-left:20px;
        padding-right:20px;
        background-color: rgba(54, 93, 122, 255);
    )");
    _highTempLineChart->installEventFilter(this);

    _lowTempLineChart = new QLabel(_rightWidget);
    _lowTempLineChart->setMinimumHeight(100);
    _lowTempLineChart->setStyleSheet(R"(
        padding-bottom: 2px;
        padding-left:20px;
        padding-right:20px;
        background-color: rgba(54, 93, 122, 255);
    )");
    _lowTempLineChart->installEventFilter(this);

    tempCurveLayout->addWidget(_highTempLineChart);
    tempCurveLayout->addWidget(_lowTempLineChart);

    // 5.风向 风力级别
    QGridLayout* gridLayoutWind = new QGridLayout();
    gridLayoutWind->setHorizontalSpacing(6);
    gridLayoutWind->setVerticalSpacing(0);
    _rightLayout->addLayout(gridLayoutWind);

    QStringList windDirectionList = {"北风", "北风", "南风", "西风", "东南风", "西北风"};
    QStringList windLevelList = {"2级", "3级", "2级", "3级", "4级", "2级"};

    for (int i = 0; i < windDirectionList.size(); ++i) {
        QLabel* windDirection = new QLabel(_rightWidget);
        QLabel* windLevel = new QLabel(_rightWidget);
        windDirection->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        windLevel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        windDirection->setText(windDirectionList[i]);
        windLevel->setText(windLevelList[i]);

        windDirection->setStyleSheet(R"(
            padding-bottom: 2px;
            padding-left: 20px;
            padding-right: 20px;
            background-color: rgb(54, 93, 122);
        )");
        windLevel->setStyleSheet(R"(
            padding-top: 2px;
            padding-left: 20px;
            padding-right: 20px;
            background-color: rgb(54, 93, 122);
        )");
        windDirection->setAlignment(Qt::AlignCenter);
        windLevel->setAlignment(Qt::AlignCenter);

        gridLayoutWind->addWidget(windDirection, 0, i);
        gridLayoutWind->addWidget(windLevel, 1, i);

        _windDirectionList << windDirection;
        _windLevelList << windLevel;
    }
}

void Widget::drawLowTempCurve()
{
    // 1.获取坐标
    int pointX[6] = {0};
    for (int i = 0; i < 6; i++) {
        pointX[i] = _airQualityList[i]->pos().x() + static_cast<int>(_airQualityList[i]->width() / 2);
    }
    int totalTemp = 0;
    int averageTemp = 0;
    for (int i = 0; i < _lowTemp.size(); i++) {
        totalTemp += _lowTemp[i];
    }
    averageTemp = static_cast<int>(totalTemp / _lowTemp.size());
    int pointY[6] = {0};
    int centerY = static_cast<int>(_lowTempLineChart->height() / 2);
    for (int i = 0; i < 6; i++) {
        pointY[i] = centerY - static_cast<int>((_lowTemp[i] - averageTemp) * INCREMENT);
    }
    // 2.绘制圆点和温度数值
    QPainter painter(_lowTempLineChart);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QPen pen = painter.pen();
    pen.setWidth(1);
    pen.setColor(QColor(0, 255, 255));
    painter.save();
    painter.setPen(pen);
    painter.setBrush(QBrush(QColor(0, 255, 255)));

    for (int i = 0; i < 6; i++) {
        painter.drawEllipse(QPoint(pointX[i], pointY[i]), POINT_RADIUS, POINT_RADIUS);
        QString tempStr = QString::number(_lowTemp[i]);
        int x = 0;
        if (tempStr.length() == 2) {
            x = pointX[i] - TEXT_OFFSET_X;
        } else {
            x = pointX[i] - TEXT_OFFSET_X + 4;
        }
        painter.drawText(QPoint(x, pointY[i] - TEXT_OFFSET_Y), tempStr);
    }
    // 3.绘制折线
    for (int i = 0; i < 5; ++i) {
        painter.drawLine(QPoint(pointX[i], pointY[i]), QPoint(pointX[i + 1], pointY[i + 1]));
    }
    painter.restore();
}

void Widget::drawHighTempCurve()
{
    // 1.获取坐标
    int pointX[6] = {0};
    for (int i = 0; i < 6; i++) {
        pointX[i] = _airQualityList[i]->pos().x() + static_cast<int>(_airQualityList[i]->width() / 2);
    }
    int totalTemp = 0;
    int averageTemp = 0;
    for (int i = 0; i < _highTemp.size(); i++) {
        totalTemp += _highTemp[i];
    }
    averageTemp = static_cast<int>(totalTemp / _highTemp.size());
    int pointY[6] = {0};
    int centerY = static_cast<int>(_highTempLineChart->height() / 2);
    for (int i = 0; i < 6; i++) {
        pointY[i] = centerY - static_cast<int>((_highTemp[i] - averageTemp) * INCREMENT);
    }
    // 2.绘制圆点和温度数值
    QPainter painter(_highTempLineChart);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QPen pen = painter.pen();
    pen.setWidth(1);
    pen.setColor(QColor(255, 170, 0));
    painter.save();
    painter.setPen(pen);
    painter.setBrush(QBrush(QColor(255, 170, 0)));

    for (int i = 0; i < 6; i++) {
        painter.drawEllipse(QPoint(pointX[i], pointY[i]), POINT_RADIUS, POINT_RADIUS);
        QString tempStr = QString::number(_highTemp[i]);
        int x = 0;
        if (tempStr.length() == 2) {
            x = pointX[i] - TEXT_OFFSET_X;
        } else {
            x = pointX[i] - TEXT_OFFSET_X + 4;
        }
        painter.drawText(QPoint(x, pointY[i] - TEXT_OFFSET_Y), tempStr);
    }
    // 3.绘制折线
    for (int i = 0; i < 5; ++i) {
        painter.drawLine(QPoint(pointX[i], pointY[i]), QPoint(pointX[i + 1], pointY[i + 1]));
    }
    painter.restore();
}

void Widget::getDate(QString& str)
{
    QDate date = QDate::currentDate();
    QString formattedDate = date.toString("yyyy/MM/dd");
    QStringList weekdays = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
    QString weekday = weekdays[date.dayOfWeek() - 1];
    str = formattedDate + " " + weekday;
}

void Widget::onSearchButtonClick()
{
    _netWork->getWeatherData(_cityEdit->text());
}
